/*
 * Author: Jeff Liu(jebberwocky@gmail.com)
 * Date: Sep 5, 2010 10:50:05 AM
 * 
 * License: MIT
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package net.citiport.ggltranslate.impl;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import net.citiport.ggltranslate.api.TranslatedTextParser;
import net.citiport.ggltranslate.ggl.TranslatedData;
import net.citiport.ggltranslate.ggl.TranslatedText;
import net.citiport.ggltranslate.util.DataUtil;

/**
 * The Class JsonTextParser.
 * using json-simple @see code.google.com/p/json-simple
 */
public class JsonTextParser implements TranslatedTextParser {

	@Override
	public TranslatedText parse(String text) {
		if(text !=null && !text.isEmpty()){
			TranslatedText tt = new TranslatedText();
			TranslatedData td = new TranslatedData();
			JSONObject object =  (JSONObject)JSONValue.parse(text);
			JSONObject rd = (JSONObject)object.get("responseData");
			td.translatedText = DataUtil.ObjectToString(rd.get("translatedText"));
			td.detectedSourceLanguage = DataUtil.ObjectToString(rd.get("detectedSourceLanguage"));
			tt.responseData = td;
			tt.responseDetails = DataUtil.ObjectToString(object.get("responseDetails"));
			tt.responseStatus = (Long)object.get("responseStatus");
			return tt;
		}else
			return null;
	}

}
